import time
from pymongo import MongoClient
from selenium import webdriver
from selenium.webdriver.common.by import By

driver=webdriver.Edge()
driver.get("https://www.zhipin.com/")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > div.search-form-con > p > input").send_keys("大数据开发工程师")
driver.find_element(By.CSS_SELECTOR, "#wrap > div.column-search-panel.search-panel-new > div > div.search-box > div.search-form > form > button").click()
time.sleep(10)
weilist=[]

for i in range(2):
    mylist = driver.find_elements(By.CSS_SELECTOR, "li")
    for li in mylist:
        try:
            mydict = {
                "工作岗位": li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-name").text,
                "工作地址": li.find_element(By.CSS_SELECTOR, ".job-title.clearfix .job-area").text,
                "工作薪资": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .salary").text,
                "工作要求": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .tag-list").text.replace("\n", "，"),
                "招聘人员": li.find_element(By.CSS_SELECTOR, ".job-info.clearfix .info-public").text,
                "公司名字": li.find_element(By.CSS_SELECTOR, ".job-card-right .company-name").text,
                "公司信息": li.find_element(By.CSS_SELECTOR, ".job-card-right ul").text,
                "工作对象": li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix ul").text,
                "工作福利": li.find_element(By.CSS_SELECTOR, ".job-card-footer.clearfix .info-desc").text
            }
            weilist.append(mydict)
        except:
            pass
    driver.find_element(By.CSS_SELECTOR, "#wrap > div.page-job-wrapper > div.page-job-inner > div > div.job-list-wrapper > div.search-job-result > div > div > div > a:nth-child(10)").click()
    time.sleep(10)

client=MongoClient("mongodb://lwjbigdata:123456@localhost:27017/?authMechanism=DEFAULT&authSource=admin")
collection=client["spiderwork"]["boss直聘"]
try:
    print("正在写入数据！")
    collection.insert_many(weilist)
except:
    print("数据写入失败！")
